package blueq.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

/**
 * 向Response写入数据，可以为text和json等等等等
 * 
 * @author 张帆
 *
 */
public class ResponseWriter {
	private static final String SUCCESS = "success";
	private static final String FAILED = "failed";

	public static void responseResource(HttpServletResponse response,
	        String contentType, String res) {
		PrintWriter out = null;
		response.setContentType(contentType);
		try {
			out = response.getWriter();
			out.write(res);
			out.flush();
		} catch (IOException ex1) {
			ex1.printStackTrace();
		} finally {
			
			out.close();
		}
	}

	/**
	 * 向response写入json数据
	 * 
	 * @param response
	 * @param json
	 */
	public static void responseJson(HttpServletResponse response, String json) {
		responseResource(response, "application/json", json);
	}

	public static void responseText(HttpServletResponse response, String text) {
		responseResource(response, "text/Xml", text);
	}

	public static void responseTextSuccess(HttpServletResponse response) {
		responseText(response, SUCCESS);
	}

	/**
	 * 向前台返回failed 
	 * @param response
	 */
	public static void responseTextFailed(HttpServletResponse response) {
		responseText(response, FAILED);
	}
}
